package cn.afactory.tygz.ui.base.views.community;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.harwkin.nb.camera.BaseImgView;
import com.harwkin.nb.camera.model.PicsItem;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.ArrayList;

import cn.afactory.tygz.R;
import cn.afactory.tygz.model.EntertainmentInfo;
import cn.afactory.tygz.ui.base.utils.ImageUtils;
import cn.afactory.tygz.ui.base.utils.NaviUtils;
import cn.afactory.tygz.ui.base.utils.StringUtils;

/**
 * Created by Kenneth on 15/9/29.
 */
public class EntertainDetailHeaderView  extends LinearLayout {

    private Context mContext;
    private EntertainmentInfo entertainmentInfo;
    private OnInfoItemClickListener onInfoItemClickListener;

    private ImageView iconInfo;
    private ImageView goodsContentImg;
    private TextView nameInfo;
    private TextView priceClassInfo;
    private RatingBar rateImage;

    private RelativeLayout locationInfoLayout;
    private TextView locationInfo;
    private RelativeLayout phoneInfoLayout;
    private TextView phoneInfo;
    private TextView briefInfoTitle;
    private TextView briefInfoContent;

    public EntertainDetailHeaderView(Context context, AttributeSet attrs, EntertainmentInfo entertainmentInfo) {
        super(context, attrs);
        mContext = context;
        this.entertainmentInfo = entertainmentInfo;
        initView(context, attrs);
    }

    public void setApartmentInfo(EntertainmentInfo entertainmentInfo) {
        this.entertainmentInfo = entertainmentInfo;
    }

    public void setOnInfoItemClickListener(OnInfoItemClickListener onInfoItemClickListener) {
        this.onInfoItemClickListener = onInfoItemClickListener;
    }

    private void initView(Context context, AttributeSet attrs){
        View rootView = LayoutInflater.from(context).inflate(R.layout.fg_entertain_detail_header, this, true);

        iconInfo = (ImageView)rootView.findViewById(R.id.icon_common_info);
        goodsContentImg = (ImageView)rootView.findViewById(R.id.iv_goods_picture);
        nameInfo = (TextView)rootView.findViewById(R.id.restaurant_title);
        priceClassInfo = (TextView)rootView.findViewById(R.id.restaurant_price_class);
        rateImage = (RatingBar)rootView.findViewById(R.id.rate_image);

        locationInfoLayout = (RelativeLayout)rootView.findViewById(R.id.location_info_layout);
        locationInfo = (TextView)rootView.findViewById(R.id.location_info);
        phoneInfoLayout = (RelativeLayout)rootView.findViewById(R.id.phone_info_layout);
        phoneInfo = (TextView)rootView.findViewById(R.id.phone_info);
        briefInfoTitle = (TextView)rootView.findViewById(R.id.brief_title);
        briefInfoContent = (TextView)rootView.findViewById(R.id.brief_content);

        goodsContentImg.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (entertainmentInfo.iconPath != null) {
                    ArrayList<PicsItem> list = new ArrayList<>();
                    PicsItem picsItem = new PicsItem();
                    picsItem.image = entertainmentInfo.iconPath;
                    list.add(picsItem);
                    NaviUtils.gotoViewBigImageActivity(mContext,list, 0, false, false);
                }
            }
        });

        bindData();
    }

    private void bindData() {

        if (null == entertainmentInfo) {
            return;
        }

        //bind data
        if (!StringUtils.isEmpty(entertainmentInfo.iconPath)) {
            goodsContentImg.setVisibility(View.VISIBLE);
            BaseImgView.loadimg(goodsContentImg,
                    ImageUtils.getBigImageFullPath(entertainmentInfo.iconPath),
                    R.drawable.ic_pic_loading_default_big,
                    R.drawable.ic_pic_loading_default_big,
                    R.drawable.ic_pic_loading_default_big,
                    ImageScaleType.EXACTLY,
                    -1,
                    -1,
                    0);
        } else {
            goodsContentImg.setVisibility(View.GONE);
        }

        //缩略图
        if (!StringUtils.isEmpty(entertainmentInfo.iconPath)) {
            iconInfo.setVisibility(View.VISIBLE);
            BaseImgView.loadimg(iconInfo,
                    entertainmentInfo.iconPath,
                    R.drawable.ic_pic_loading_default_small,
                    R.drawable.ic_pic_loading_default_small,
                    R.drawable.ic_pic_loading_default_small,
                    ImageScaleType.EXACTLY,
                    -1,
                    -1,
                    0);
        } else {
            iconInfo.setImageResource(R.drawable.ic_pic_loading_default_small);
        }

        if (!TextUtils.isEmpty(entertainmentInfo.name)) {
            nameInfo.setText(entertainmentInfo.name);
        } else {
            nameInfo.setText("");
        }

        if (!TextUtils.isEmpty(entertainmentInfo.typeName) && entertainmentInfo.price > 0) {
            priceClassInfo.setText("￥"+ entertainmentInfo.price +"/人 " + entertainmentInfo.typeName);
        } else if (entertainmentInfo.price > 0){
            priceClassInfo.setText("￥"+ entertainmentInfo.price +"/人 ");
        } else {
            priceClassInfo.setText(" ");
        }
        //rate
        rateImage.setRating((float)entertainmentInfo.score);

        if (!TextUtils.isEmpty(entertainmentInfo.address)) {
            locationInfo.setText(entertainmentInfo.address);
            locationInfoLayout.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != onInfoItemClickListener) {
                        onInfoItemClickListener.onLocationInfoClick(entertainmentInfo);
                    }
                }
            });
        } else {
            locationInfo.setText("");
        }

        if (!TextUtils.isEmpty(entertainmentInfo.tel)) {
            phoneInfo.setText(entertainmentInfo.tel);
            phoneInfoLayout.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != onInfoItemClickListener) {
                        onInfoItemClickListener.onPhoneNumInfoClick(entertainmentInfo.tel);
                    }
                }
            });
        } else {
            phoneInfo.setText("");
        }

        if (!TextUtils.isEmpty(entertainmentInfo.intro)) {
            briefInfoContent.setText(entertainmentInfo.intro);
        } else {
            briefInfoContent.setText("暂无简介");
        }

    }

    public interface OnInfoItemClickListener
    {
        void onLocationInfoClick(EntertainmentInfo entertainmentInfo);
        void onPhoneNumInfoClick(String phoneNum);
    }
}
